﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class NumInfoUi : BattleInfoUI {

    public Color addColor = Color.white;
    public Color minusColor = Color.red;

    [SerializeField]
    Text numText;

    [SerializeField]
    AnimationCurve m_Curve;

    Vector3 m_TargetUVPos;

    // Use this for initialization
    protected override void Start () {
        gameObject.AddComponent<DelayDestroy>().lifeTime = 1;

       // var rect = GetComponent<RectTransform>();
       // StartCoroutine(PlayRectMove(m_Reference,3, m_TargetUVPos));
    }

    public void Init(int showNum, Transform refer, Func<Vector3, Vector3, Vector3> worldToUIPosFunc, Vector3 offset, bool moveUp, Vector3 moveTargetUVPos, bool follow)
    {
        base.Init(refer, worldToUIPosFunc);

        m_TargetUVPos = moveTargetUVPos;
        m_Offset = offset;

        if (numText)
        {
            if (showNum >= 0)
            {
                numText.text = string.Format("+ {0}", showNum);
                //numText.color = this.addColor;
            }
            else
            {
                numText.text = string.Format("- {0}", Mathf.Abs(showNum));
                numText.color = this.minusColor;
            }

        }
        SetPosition(GetComponent<RectTransform>(),this.m_WorldToUIPosFunc(refer.position, m_Offset));

        if(follow)
        {
            if (moveUp)
            {
                var rect = GetComponent<RectTransform>();
                StartCoroutine(PlayRectMove(m_Reference, 1, m_TargetUVPos));
            }
            else
            {
                StartCoroutine(UpdatePos(m_Reference));
            }
        }
        else
        {
            if (moveUp)
            {
                var rect = GetComponent<RectTransform>();
                StartCoroutine(PlayRectMove(null, 1, m_TargetUVPos));
            }
        }
    }

    IEnumerator UpdatePos(Transform refer)
    {
        var rect = GetComponent<RectTransform>();
        while (true)
        {
            yield return 0;

            Vector3 targetPos = m_WorldToUIPosFunc(refer.transform.position, m_Offset);
            SetPosition(rect, targetPos);
        }
    }
    
    IEnumerator PlayRectMove(Transform refer, float time,Vector3 targetUVPos)
    {
        if (m_WorldToUIPosFunc == null) yield break;

        float timer = 0;
        var rect = GetComponent<RectTransform>();

        Vector3 beginPos = transform.position;

        while (timer <= time)
        { 
            Vector3 targetPos = Vector3.zero;
            if (refer)
            {
                targetPos = m_WorldToUIPosFunc(refer.transform.position, m_Offset) + targetUVPos; // + new Vector3(0,300,0);
            }
            else
            {
                targetPos = beginPos + targetUVPos;
                
            }
            
            //targetPos.z = 10;
            timer += Time.deltaTime;

            float speed = m_Curve.Evaluate(timer / time);

            float y = Mathf.Lerp(beginPos.y, targetPos.y, speed);
            
            //GameDebug.LogError($"targetPos:{targetPos}  beginPos:{beginPos} ");
            
            var curPos = new Vector3(targetPos.x, y, 0);
            SetPosition(rect, curPos);
            yield return null;
        }

    }

}
